<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    //个人中心
    public function index(User $user)
    {
        //这个人的信息 用户名  头像 关注  粉丝 数
        $user = User::withCount(['fans','posts','stars'])->find($user->id);
        //这个人的文章列表
        $posts = $user->posts()->orderBy('created_at','desc')->take(10)->get();
        //这个人关注的用户
        $stars = $user->stars();

        $susers = User::whereIn('id',$stars->pluck('star_id'))->withCount(['posts','fans','stars'])->get();
        //这个人的粉丝
        $fans = $user->fans();
        $fusers = User::whereIn('id',$fans->pluck('fan_id'))->withCount(['posts','fans','stars'])->get();
        return view('user/index',compact('user','posts','susers','fusers'));
    }


    //个人中心设置页面
    public function setting()
    {
        return view('user/setting');
    }

    //个人中心设置行为
    public function settingStore()
    {

    }

    //个人中心关注某人
    public function fan(User $user)
    {
        $me = \Auth::user();
        \App\Fan::firstOrCreate(['fan_id' => $me->id, 'star_id' => $user->id]);
        return [
            'error' => 0,
            'msg' => ''
        ];
    }


    //个人中心取消关注某人
    public function unFan(User $user)
    {
        $me = \Auth::user();
        \App\Fan::where('fan_id', $me->id)->where('star_id', $user->id)->delete();
        return [
            'error' => 0,
            'msg' => ''
        ];
    }

}
